package com.hzx.ssm.controller;

import com.hzx.ssm.entity.Admin;
import com.hzx.ssm.entity.ResBean;
import com.hzx.ssm.service.AdminService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;


@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    //管理员登录
    @ApiOperation("用户登录")
    @ResponseBody
    @PostMapping(value = "login")
    public ResBean login(@RequestParam("username") String username,@RequestParam("password") String password) {

        return adminService.login(username,password);
    }


    @ApiOperation("管理员修改密码")
    @ResponseBody
    @PutMapping(value = "updatePassword")
    public ResBean updatePassword(@RequestParam("manage_id") String manager_id,@RequestParam("password")
            String password) {
        return adminService.updatePassword(manager_id,password);
    }

    @ApiOperation("管理员根据id修改个人信息")
    @ResponseBody
    @PutMapping(value = "updateSelfMessage",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResBean updateSelfMessage(Admin admin, MultipartFile faceImg) throws IOException {
        //下载图片
        String originalFilename = faceImg.getOriginalFilename();
        //获取图片的后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf('.'));
        String prefix = UUID.randomUUID().toString();
        //创建用户图片保存的路径
        String fullPath = "/userFace/" +  prefix+suffix;
        faceImg.transferTo(new File(fullPath));
        admin.setFace(fullPath);
        return adminService.updateSelfMessage(admin);
    }

    @ApiOperation("根据Id获取用户的所有信息")
    @ResponseBody
    @GetMapping("/getAdminInfo")
    public ResBean getAdminInfo(@RequestParam("manager_id") String manager_id){
        return adminService.getAdminInfo(manager_id);
    }


}
